﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EasyCode.Utility;
using BusinessHandler;
public partial class Page_RoleAssignment : System.Web.UI.Page
{
    private UserController userControl;
    private ModuleController moduleController;
    private UserInModuleController userInModuleController;
    //private int currentUserID = -1;
    private List<UsersInModule> userInModuleList;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.LoadUser();
            this.GetModuleList();
        }
    }
    protected void Repeater_ModuleList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

    }
    private void GetUserInModule()
    {
        userInModuleController = UserInModuleController.Instance;
        if (ListBox_UserList.Items.Count < 1)
        {
            Response.Redirect("AccessDeny.aspx");
        }
        this.userInModuleList = userInModuleController.GetUserInModule(Protector.Int(this.ListBox_UserList.SelectedItem.Value, -1));
    }
    private void GetModuleList()
    {
        this.GetUserInModule();
        this.moduleController = ModuleController.Instance;
        this.Repeater_ModuleList.DataSource = this.moduleController.GetModuleList();
        this.Repeater_ModuleList.DataBind();
        this.SetRoleValue();

    }
    protected void ListBox_UserList_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.GetModuleList();
    }
    private void LoadUser()
    {
        userControl = UserController.Instance;
        string userName = null;
        if (!string.IsNullOrEmpty(this.TextBox_UserName.Text))
        {
            userName = this.TextBox_UserName.Text.Trim();
        }
        List<User> userList = userControl.GetUserList(userName);
        if (userList != null)
        {
            for (int i = userList.Count - 1; i >= 0; i--)
            {
                if (Protector.Int(userList[i].UserType, 0) == (int)UserType.Admin)
                {
                    userList.RemoveAt(i);
                }
            }
            if (userList.Count > 0)
            {
                this.ListBox_UserList.DataSource = userList;
                this.ListBox_UserList.DataTextField = "Username";
                this.ListBox_UserList.DataValueField = "UserID";
                this.ListBox_UserList.DataBind();
                if (Session["CurrectSelectUser"] != null)
                {
                    for (int j = 0; j < this.ListBox_UserList.Items.Count; j++)
                    {
                        if (this.ListBox_UserList.Items[j].Value == Session["CurrectSelectUser"].ToString())
                        {
                            this.ListBox_UserList.Items[j].Selected = true;
                            break;
                        }
                    }
                }
                else
                {
                    this.ListBox_UserList.SelectedIndex = 0;
                }
                if (ListBox_UserList.DataSource == null)
                {
                    Response.Redirect("AccessDeny.aspx");
                    //this.currentUserID = Protector.Int(this.ListBox_UserList.SelectedItem.Value, -1);
                }

            }
            else
            {
                Response.Redirect("AccessDeny.aspx");
            }
        }
    }
    protected void UpdatePanelContent_Load(object sender, EventArgs e)
    {
        //string arg = Request.Form["__EVENTARGUMENT"];
        //if (string.IsNullOrEmpty(arg)) return;
        //if (arg != null)
        //{
        //    string[] temp = arg.Split('|');
        //    if (temp.Length == 2)
        //    {
        //        if (temp[1].ToString().ToUpper() == "PAGE")
        //        {
        //            this.currentPage = Protector.Int(temp[0].ToString(), 0);
        //            this.LoadList();
        //        }
        //    }
        //}
    }
    protected void Button_SaveAllButtom_Click(object sender, EventArgs e)
    {
        this.GetUserInModule();
        for (int i = 0; i < this.Repeater_ModuleList.Items.Count; i++)
        {
            Literal Literal_ID = Repeater_ModuleList.Items[i].FindControl("Literal_ID") as Literal;
            if (Literal_ID != null)
            {
                CheckBox CheckBox_All = Repeater_ModuleList.Items[i].FindControl("CheckBox_All") as CheckBox;
                CheckBox CheckBox_IsDeny = Repeater_ModuleList.Items[i].FindControl("CheckBox_IsDeny") as CheckBox;
                CheckBox CheckBox_Isview = Repeater_ModuleList.Items[i].FindControl("CheckBox_Isview") as CheckBox;
                CheckBox CheckBox_IsAdd = Repeater_ModuleList.Items[i].FindControl("CheckBox_IsAdd") as CheckBox;
                CheckBox CheckBox_IsEdit = Repeater_ModuleList.Items[i].FindControl("CheckBox_IsEdit") as CheckBox;
                CheckBox CheckBox_IsDelete = Repeater_ModuleList.Items[i].FindControl("CheckBox_IsDelete") as CheckBox;
                UsersInModule item = null;
                if (this.userInModuleList != null)
                {
                    item = (from o in this.userInModuleController.UsersInModules where o.ModuleID == Protector.Int(Literal_ID.Text) && o.UserID == Protector.Int(this.ListBox_UserList.SelectedItem.Value, -1) select o).SingleOrDefault();
                }
                if (item == null)
                {
                    if (!CheckRoles.CanInsert())
                    {
                        Response.Redirect(ResolveUrl("~/Page/AccessDeny.aspx"));
                    }
                    UsersInModule insert = new UsersInModule();
                    insert.ModuleID = Protector.Int(Literal_ID.Text);
                    insert.UserID = Protector.Int(this.ListBox_UserList.SelectedItem.Value, -1);
                    if (CheckBox_All.Checked)
                    {
                        insert.IsFull = true;
                    }
                    else if (CheckBox_IsDeny.Checked)
                    {
                        insert.IsDeny = true;
                    }
                    else
                    {
                        insert.IsView = CheckBox_Isview.Checked;
                        insert.IsAdd = CheckBox_IsAdd.Checked;
                        insert.IsEdit = CheckBox_IsEdit.Checked;
                        insert.IsDelete = CheckBox_IsDelete.Checked;
                    }
                    try
                    {
                        UserInModuleController.Instance = null;
                        userInModuleController = UserInModuleController.Instance;
                        userInModuleController.UsersInModules.InsertOnSubmit(insert);
                        userInModuleController.SubmitChanges();
                        this.Message_Alert.SetSuccess(string.Format(Resources.SystemAccount.PermissionUpdatedSuccess, this.ListBox_UserList.SelectedItem.Text));
                    }
                    catch (Exception ex)
                    {
                        this.Message_Alert.SetError(string.Format(Resources.SystemAccount.PermissionUpdateFail, this.ListBox_UserList.SelectedItem.Text, ex.Message));
                    }
                }
                else
                {
                    if (!CheckRoles.CanEdit())
                    {
                        Response.Redirect(ResolveUrl("~/Page/AccessDeny.aspx"));
                    }
                    if (CheckBox_IsDeny.Checked)
                    {
                        item.IsDeny = true;
                        item.IsView = false;
                        item.IsAdd = false;
                        item.IsEdit = false;
                        item.IsDelete = false;
                        item.IsFull = false;
                    }
                    else if (CheckBox_All.Checked)
                    {
                        item.IsDeny = false;
                        item.IsView = true;
                        item.IsAdd = true;
                        item.IsEdit = true;
                        item.IsDelete = true;
                        item.IsFull = true;
                    }
                    else
                    {
                        item.IsFull = false;
                        item.IsDeny = false;
                        item.IsView = CheckBox_Isview.Checked;
                        item.IsAdd = CheckBox_IsAdd.Checked;
                        item.IsEdit = CheckBox_IsEdit.Checked;
                        item.IsDelete = CheckBox_IsDelete.Checked;
                    }
                    try
                    {
                        this.userInModuleController.SubmitChanges();
                        this.Message_Alert.SetSuccess(string.Format(Resources.SystemAccount.PermissionUpdatedSuccess, this.ListBox_UserList.SelectedItem.Text));
                    }
                    catch (Exception ex)
                    {
                        this.Message_Alert.SetError(string.Format(Resources.SystemAccount.PermissionUpdateFail, this.ListBox_UserList.SelectedItem.Text, ex.Message));
                    }
                }
            }
        }
        UserInModuleController.Instance = null;
        this.GetModuleList();
    }
    public bool? Set(object obj)
    {
        if (obj != null)
        {
            return Protector.Bool(obj);
        }
        return false;
    }
    private void SetRoleValue()
    {
        for (int i = 0; i < this.Repeater_ModuleList.Items.Count; i++)
        {
            Literal Literal_ID = Repeater_ModuleList.Items[i].FindControl("Literal_ID") as Literal;
            if (Literal_ID != null)
            {
                if (userInModuleList != null)
                {
                    UsersInModule item = (from o in this.userInModuleList where o.ModuleID == Protector.Int(Literal_ID.Text) select o).SingleOrDefault();
                    CheckBox CheckBox_All = Repeater_ModuleList.Items[i].FindControl("CheckBox_All") as CheckBox;
                    CheckBox CheckBox_IsDeny = Repeater_ModuleList.Items[i].FindControl("CheckBox_IsDeny") as CheckBox;
                    CheckBox CheckBox_Isview = Repeater_ModuleList.Items[i].FindControl("CheckBox_Isview") as CheckBox;
                    CheckBox CheckBox_IsAdd = Repeater_ModuleList.Items[i].FindControl("CheckBox_IsAdd") as CheckBox;
                    CheckBox CheckBox_IsEdit = Repeater_ModuleList.Items[i].FindControl("CheckBox_IsEdit") as CheckBox;
                    CheckBox CheckBox_IsDelete = Repeater_ModuleList.Items[i].FindControl("CheckBox_IsDelete") as CheckBox;
                    if (item != null)
                    {
                        if (item.IsFull != null && item.IsFull.Value)
                        {
                            CheckBox_All.Checked = true;
                            CheckBox_Isview.Checked = true;
                            CheckBox_IsAdd.Checked = true;
                            CheckBox_IsEdit.Checked = true;
                            CheckBox_IsDelete.Checked = true;
                        }
                        else if (item.IsDeny != null && item.IsDeny.Value)
                        {
                            CheckBox_IsDeny.Checked = true;
                        }
                        else
                        {
                            if (item.IsView != null)
                            {
                                CheckBox_Isview.Checked = item.IsView.Value;
                            }
                            if (item.IsAdd != null)
                            {
                                CheckBox_IsAdd.Checked = item.IsAdd.Value;
                            }
                            if (item.IsEdit != null)
                            {
                                CheckBox_IsEdit.Checked = item.IsEdit.Value;
                            }
                            if (item.IsDelete != null)
                            {
                                CheckBox_IsDelete.Checked = item.IsDelete.Value;
                            }
                        }
                    }
                }
            }
        }
    }
    int id = 1;
    protected void Repeater_ModuleList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e != null)
        {
            CheckBox CheckBox_All = e.Item.FindControl("CheckBox_All") as CheckBox;
            CheckBox CheckBox_Isview = e.Item.FindControl("CheckBox_Isview") as CheckBox;
            CheckBox CheckBox_IsAdd = e.Item.FindControl("CheckBox_IsAdd") as CheckBox;
            CheckBox CheckBox_IsEdit = e.Item.FindControl("CheckBox_IsEdit") as CheckBox;
            CheckBox CheckBox_IsDelete = e.Item.FindControl("CheckBox_IsDelete") as CheckBox;
            CheckBox CheckBox_IsDeny = e.Item.FindControl("CheckBox_IsDeny") as CheckBox;
            if (CheckBox_All != null)
            {
                CheckBox_All.Attributes.Add("OnClick", "return Check('" + CheckBox_All.ClientID + ";" + CheckBox_Isview.ClientID + ";" + CheckBox_IsAdd.ClientID + ";" + CheckBox_IsEdit.ClientID + ";" + CheckBox_IsDelete.ClientID + ";" + CheckBox_IsDeny.ClientID + "');");
                CheckBox_IsDeny.Attributes.Add("OnClick", "return UnCheck('" + CheckBox_All.ClientID + ";" + CheckBox_Isview.ClientID + ";" + CheckBox_IsAdd.ClientID + ";" + CheckBox_IsEdit.ClientID + ";" + CheckBox_IsDelete.ClientID + ";" + CheckBox_IsDeny.ClientID + "');");


                CheckBox_Isview.Attributes.Add("OnClick", "return CheckOne('" + CheckBox_All.ClientID + ";" + CheckBox_Isview.ClientID + ";" + CheckBox_IsAdd.ClientID + ";" + CheckBox_IsEdit.ClientID + ";" + CheckBox_IsDelete.ClientID + ";" + CheckBox_IsDeny.ClientID + "');");
                CheckBox_IsAdd.Attributes.Add("OnClick", "return CheckOne('" + CheckBox_All.ClientID + ";" + CheckBox_Isview.ClientID + ";" + CheckBox_IsAdd.ClientID + ";" + CheckBox_IsEdit.ClientID + ";" + CheckBox_IsDelete.ClientID + ";" + CheckBox_IsDeny.ClientID + "');");
                CheckBox_IsEdit.Attributes.Add("OnClick", "return CheckOne('" + CheckBox_All.ClientID + ";" + CheckBox_Isview.ClientID + ";" + CheckBox_IsAdd.ClientID + ";" + CheckBox_IsEdit.ClientID + ";" + CheckBox_IsDelete.ClientID + ";" + CheckBox_IsDeny.ClientID + "');");
                CheckBox_IsDelete.Attributes.Add("OnClick", "return CheckOne('" + CheckBox_All.ClientID + ";" + CheckBox_Isview.ClientID + ";" + CheckBox_IsAdd.ClientID + ";" + CheckBox_IsEdit.ClientID + ";" + CheckBox_IsDelete.ClientID + ";" + CheckBox_IsDeny.ClientID + "');");
            }
        }
    }
    protected void Button_Search_Click(object sender, EventArgs e)
    {
        this.LoadUser();
    }

    protected void Button_ViewAll_Click(object sender, EventArgs e)
    {
        this.TextBox_UserName.Text = string.Empty;
        this.LoadUser();
    }
}